package ge.telasi.tasks.ui.comp;

import java.util.Calendar;
import static java.util.Calendar.*;
import javax.swing.JSpinner;

/**
 * Single date calendar.
 *
 * @author dimitri
 */
public class CalendarControl extends javax.swing.JPanel {
    public static final String[] MONTHS = {"იანვარი", "თებერვალი", "მარტი", "აპრილი", "მაისი", "ივნისი", "ივლისი", "აგვისტო", "სექტემბერი", "ოქტომბერი", "ნოემბერი", "დეკემბერი"};

    /** Creates new form FCalendar */
    public CalendarControl() {
        initComponents();
        setDate(null);
        spYear.setEditor(new JSpinner.NumberEditor(spYear, "####"));
        calendarGrid.setFCalendar(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmbMonth = new javax.swing.JComboBox();
        spYear = new javax.swing.JSpinner();
        javax.swing.JSpinner.NumberEditor yearEditor = (javax.swing.JSpinner.NumberEditor) spYear.getEditor();
        yearEditor.getTextField().setColumns(4);
        spYear.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent e) {
                spYearChange(e);
            }
        });

        calendarGrid = new ge.telasi.tasks.ui.comp.CalendarGrid();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cmbMonth.setMaximumRowCount(12);
        cmbMonth.setModel(new javax.swing.DefaultComboBoxModel(MONTHS));
        cmbMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMonthActionPerformed(evt);
            }
        });

        spYear.setModel(new javax.swing.SpinnerNumberModel(2008, 1000, 9999, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cmbMonth, 0, 99, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spYear, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(calendarGrid, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(calendarGrid, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmbMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMonthActionPerformed
        Calendar currDate = getDate();
        currDate.set(MONTH, cmbMonth.getSelectedIndex());
        setDate(currDate);
    }//GEN-LAST:event_cmbMonthActionPerformed

    private void spYearChange(javax.swing.event.ChangeEvent evt) {
        Calendar currDate = getDate();
        currDate.set(YEAR, (Integer) spYear.getValue());
        setDate(currDate);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected ge.telasi.tasks.ui.comp.CalendarGrid calendarGrid;
    protected javax.swing.JComboBox cmbMonth;
    protected javax.swing.JSpinner spYear;
    // End of variables declaration//GEN-END:variables

    public void setDate(Calendar date) {
        if (date == null) {
            date = Calendar.getInstance();
        }
        int year = date.get(YEAR);
        int month = date.get(MONTH);
        calendarGrid.setDate(date);
        cmbMonth.setSelectedIndex(month);
        spYear.setValue(year);
    }

    public Calendar getDate() {
        return calendarGrid.getDate();
    }

}
